package astar 
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	/**
	 * AStar 网格视图类
	 * @author StefanieKaka
	 */
	public class GridView extends Sprite 
	{
		/// 构造函数
		public function GridView(grid : Grid) 
		{
			m_grid = grid;
			drawGrid();
			findPath();
			this.addEventListener(MouseEvent.CLICK, onGridClick);
			
			for (var i : int = 0; i < m_grid.cols; ++i)
			{
				var txt : TextField = new TextField();
				txt.text = i.toString();
				txt.x = m_cellSize * i + txt.textWidth / 2;
				txt.y = -15;
				txt.mouseEnabled = false;
				txt.selectable = false;
				this.addChild(txt);
			}
			
			for (i = 0; i < m_grid.rows; ++i)
			{
				txt = new TextField();
				txt.text = i.toString();
				txt.x = -15;
				txt.y = m_cellSize * i;
				txt.mouseEnabled = false;
				txt.selectable = false;
				this.addChild(txt);
			}
		}
		
///////////////////////////////////////////////////////////////////////////////////////////////////
// 公共域
///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/// 绘制网格
		public function drawGrid() : void
		{
			this.graphics.clear();
			for (var i : int = 0; i < m_grid.cols; ++i)
			{
				for (var j : int = 0; j < m_grid.rows; ++j)
				{
					var node : Node = m_grid.getNode(i, j);
					this.graphics.lineStyle(0);
					this.graphics.beginFill(getColor(node));
					this.graphics.drawRect(i * m_cellSize, j * m_cellSize, m_cellSize, m_cellSize);
					this.graphics.endFill();
				}
			}
		}

///////////////////////////////////////////////////////////////////////////////////////////////////
// 私有域
///////////////////////////////////////////////////////////////////////////////////////////////////

		/// 根据格子对象获得格子的颜色
		private function getColor(node : Node) : uint
		{
			if (!node.walkable)
				return 0x000000;
				
			if (node == m_grid.startNode)
				return 0x666666;
				
			if (node == m_grid.endNode)
				return 0x666666;
				
			return 0xffffff;
		}
		
		/// 鼠标点击事件
		private function onGridClick(event : MouseEvent) : void
		{
			var xpos : int = Math.floor(event.localX / m_cellSize);
			var ypos : int = Math.floor(event.localY / m_cellSize);
			
			m_grid.setWalkable(xpos, ypos, !m_grid.getNode(xpos, ypos).walkable);
			
			drawGrid();
			findPath();
		}
		
		/// 搜寻路径
		private function findPath() : void
		{
			var astarFind : AStar = new AStar();
			if (astarFind.findPath(m_grid, AStar.MANHATTAN))
			{
				showVisited(astarFind);
				showPath(astarFind);
			}
		}
		
		/// 高亮所有被搜索过的节点
		private function showVisited(astarFind : AStar) : void
		{
			var visited : Vector.<Node> = astarFind.visited;
			for (var i : int = 0; i < visited.length; ++i)
			{
				this.graphics.beginFill(0xcccccc);
				this.graphics.drawRect(visited[i].x * m_cellSize, visited[i].y * m_cellSize, m_cellSize, m_cellSize);
				this.graphics.endFill();
			}
		}
		
		/// 找到的路径高亮
		private function showPath(astarFind : AStar) : void
		{
			this.graphics.lineStyle(0);
			
			var path : Vector.<Node> = astarFind.path;
			for (var i : int = 0; i < path.length; ++i)
			{
				this.graphics.beginFill(0xffffff);
				this.graphics.drawCircle(path[i].x * m_cellSize + m_cellSize / 2,
											path[i].y * m_cellSize + m_cellSize / 2, 
											m_cellSize / 3);
				this.graphics.endFill();
			}
		}
		
		/// 显示已查找列表的中的节点
		private function showChecked(astarFind : AStar) : void
		{
			
		}
		
		private var m_grid : Grid = null;
		private var m_cellSize : int = 20;
		
	}

}